package com.chuangjiangx.member.basic.ddd.domain.model;

import com.chuangjiangx.dddbase.Repository;
import com.chuangjiangx.member.basic.dao.mapper.InMbrUserMappingMapper;
import com.chuangjiangx.member.basic.dao.model.InMbrUserMapping;
import com.chuangjiangx.member.basic.dao.model.InMbrUserMappingExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.Optional;

/**
 * Created by zhoufushou on 2017/12/20.
 * 会员用户关系资源库
 */
@Component
public class MbrUserMappingRepository implements Repository<MbrUserMapping, MbrUserMappingId> {

    @Autowired
    private InMbrUserMappingMapper inMbrUserMappingMapper;


    @Override
    public MbrUserMapping fromId(MbrUserMappingId mbrUserMappingId) {
        InMbrUserMapping inMbrUserMapping = inMbrUserMappingMapper.selectByPrimaryKey(mbrUserMappingId.getId());
        if (inMbrUserMapping == null) {
            return null;
        }
        return wrap(inMbrUserMapping);
    }

    @Override
    public void update(MbrUserMapping mbrUserMapping) {
        InMbrUserMapping in = unbox(mbrUserMapping);
        in.setUpdateTime(new Date());
        inMbrUserMappingMapper.updateByPrimaryKeySelective(in);
    }

    @Override
    public void save(MbrUserMapping mbrUserMapping) {
        InMbrUserMapping in = unbox(mbrUserMapping);
        in.setCreateTime(new Date());
        inMbrUserMappingMapper.insertSelective(in);
    }

    public MbrUserMapping from(MemberId memberId, MerchantId merchantId, MbrUserMappingType type) {
        InMbrUserMappingExample example = new InMbrUserMappingExample();
        example.createCriteria()
                .andMemberIdEqualTo(memberId.getId())
                .andMerchantIdEqualTo(merchantId.getId())
                .andTypeEqualTo(type.value);
        List<InMbrUserMapping> inMbrUserMappings = inMbrUserMappingMapper.selectByExample(example);
        if (inMbrUserMappings.isEmpty()) {
            return null;
        }
        InMbrUserMapping inMbrUserMapping = inMbrUserMappings.get(0);
        MbrUserMapping mbrUserMapping = new MbrUserMapping(
                memberId, merchantId,
                inMbrUserMapping.getOpenId(),inMbrUserMapping.getMopenId(),
                type, inMbrUserMapping.getAopenId(), inMbrUserMapping.getAiFaceImg());
        mbrUserMapping.setId(new MbrUserMappingId(inMbrUserMapping.getId()));
        return mbrUserMapping;
    }

    public MbrUserMapping from(String openid, MerchantId merchantId,MbrUserMappingType type) {
        InMbrUserMappingExample example = new InMbrUserMappingExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId.getId())
                .andOpenIdEqualTo(openid)
                .andTypeEqualTo(type.value);
        List<InMbrUserMapping> inMbrUserMappings = inMbrUserMappingMapper.selectByExample(example);
        if (inMbrUserMappings.isEmpty()) {
            return null;
        }
        InMbrUserMapping inMbrUserMapping = inMbrUserMappings.get(0);
        return wrap(inMbrUserMapping);
    }

    private MbrUserMapping wrap(InMbrUserMapping in) {
        MbrUserMapping mbrUserMapping = new MbrUserMapping(
                new MemberId(in.getMemberId()),
                new MerchantId(in.getMerchantId()),
                in.getOpenId(),
                in.getMopenId(),
                MbrUserMappingType.fromValue(in.getType()), in.getAopenId(), in.getAiFaceImg());
        mbrUserMapping.modifyAopenId(in.getAopenId());
        mbrUserMapping.setId(new MbrUserMappingId(in.getId()));
        return mbrUserMapping;
    }

    private InMbrUserMapping unbox(MbrUserMapping m) {
        InMbrUserMapping in = new InMbrUserMapping();
        Optional<MbrUserMapping> optional = Optional.of(m);
        in.setId(optional.map(o -> o.getId()).map(o -> o.getId()).orElse(null));
        in.setMemberId(optional.map(o -> o.getMemberId()).map(o -> o.getId()).orElse(null));
        in.setMerchantId(optional.map(o -> o.getMerchantId()).map(o -> o.getId()).orElse(null));
        in.setOpenId(optional.map(o -> o.getOpenId()).orElse(null));
        in.setMopenId(optional.map(o -> o.getMopenId()).orElse(null));
        in.setType(optional.map(o -> o.getType()).map(o -> o.value).orElse(null));
        in.setUpdateTime(optional.map(o -> o.getTimestamp()).map(o -> o.getUpdateTime()).orElse(null));
        in.setAopenId(optional.map(o -> o.getAopenId()).orElse(null));
        in.setAiFaceImg(optional.map(o -> o.getAiFaceImg()).orElse(null));
        return in;
    }

    public void deleteByMemberId(MemberId memberId, MerchantId merchantId) {
        InMbrUserMappingExample example = new InMbrUserMappingExample();
        example.createCriteria().andMemberIdEqualTo(memberId.getId()).andMerchantIdEqualTo(merchantId.getId());
        List<InMbrUserMapping> list = inMbrUserMappingMapper.selectByExample(example);
        if (list != null && list.size() > 0) {
            MbrUserMapping mbrUserMapping = wrap(list.get(0));
            inMbrUserMappingMapper.deleteByPrimaryKey(mbrUserMapping.getId().getId());
        }
    }




    public boolean checkOpenId(String openid) {
        InMbrUserMappingExample example = new InMbrUserMappingExample();
        example.createCriteria().andOpenIdEqualTo(openid);
        List<InMbrUserMapping> list = inMbrUserMappingMapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return true;
        } else {
            return false;
        }
    }

    public MbrUserMapping getInMbrUserMappingByAopenId(Long merchantId,MbrUserMappingType type,String aopenId) {
        InMbrUserMappingExample example = new InMbrUserMappingExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId)
                .andTypeEqualTo(type.value)
                .andAopenIdEqualTo(aopenId);
        List<InMbrUserMapping> list = inMbrUserMappingMapper.selectByExample(example);
        if (list == null || list.size() == 0) {
            return null;
        }
        return wrap(list.get(0));
    }

}
